<?php 
#pdo數據庫連接基類
class lib_db_pdo{
    
    private $pdo;
    private $config;
    
	public function __construct($config)
	{
		$this->config=$config;
	}    
    
    public function connect(){
        try{          
           $this->pdo= new PDO("mysql:dbname=$this->config['db_name'];host=$this->config['db_host'];charset=$this->config['db_charset']","$this->config['db_username']","$this->config['db_password']",array(PDO::ATTR_PERSISTENT => true));
        }catch (PDOException $e)  {
            die( $e->getMessage() );
        }
        $this->pdo->query("SET NAME {$this->config['db_charset']}");
    }
    
    public function query($sql){
        try{
           $reulst= $this->pdo->exec($sql);
        }catch (PDOException $e){
            die($e->getMessage());
        }
        return $reulst;
    }
    
    public function num_rows($sql){
        $reulst=$this->fetch_array($sql);
        return count($reulst);
    }
    
    public function insert_id(){
        return $this->pdo->lastInsertId();
    }
    
    public function fetch_array($sql,$type='FETCH_ASSOC'){      
       $result=array();
       $rs=$this->pdo->query($sql);
       $this->pdo->setFetchMode(PDO::$type);
       $result=$rs->fetchAll();
       return $result;
    }
    
    public function fetch_value($sql,$type='FETCH_ASSOC'){
        $this->pdo->setFetchMode(PDO::$type);
        $this->pdo->query($sql);
        return $this->pdo->fetch();
    }
    
	public function  getValue($sql){
	    return $this->fetch_value($sql);
	}   
    
}
?>